package com.dream.xcarrecorder.data;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.dream.xcarrecorder.MP4PlayerActivity;
import com.dream.xcarrecorder.R;
import com.dream.xcarrecorder.VideoActivity;

import java.io.File;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

public class RecAdapter extends BaseAdapter {

    private Context mContext;
    private List<RecItem> itemList;

    public RecAdapter(Context context) {
        this.mContext = context;
        this.itemList = new ArrayList<>();
    }

    @Override
    public int getCount() {
        return itemList.size();
    }

    @Override
    public RecItem getItem(int position) {
        return itemList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView =
                    LayoutInflater.from(mContext).inflate(R.layout.item_list_rec,
                            parent, false);
            holder = new ViewHolder();
            holder.fileName = convertView.findViewById(R.id.item_rec_name);
            holder.fileSize = convertView.findViewById(R.id.item_rec_size);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        RecItem item = itemList.get(position);

        holder.fileName.setText(item.fileName.substring(item.fileName.lastIndexOf("/")+1));
        holder.fileSize.setText(item.fileSize);

        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                VideoActivity.startActivity(mContext,item.fileName);
                MP4PlayerActivity.startActivity(mContext,
                        item.fileName);
            }
        });

        return convertView;
    }

    static class ViewHolder {
        TextView fileName;
        TextView fileSize;
    }

    public void loadData(String folderPath) {
        itemList.clear();
        File folder = new File(folderPath);
        if (folder.exists() && folder.isDirectory()) {
            File[] files = folder.listFiles();
            if (files!= null) {
                DecimalFormat decimalFormat = new DecimalFormat("#.##");
                for (File file : files) {
                    long fileSize = file.length();
                    String sizeString;
                    if (fileSize < 1024) {
                        sizeString = fileSize + " B";
                    } else if (fileSize < 1024 * 1024) {
                        sizeString = decimalFormat.format((double) fileSize / 1024) + " KB";
                    } else {
                        sizeString = decimalFormat.format((double) fileSize / (1024 * 1024)) + " MB";
                    }
                    itemList.add(new RecItem(file.getAbsolutePath(), sizeString));
                }
                notifyDataSetChanged();
            }
        } else {
            Log.e("RecAdapter", "指定路径不是一个有效的文件夹。");
        }
    }
}